package gal.util;

import gal.obj.Body2D;
import gal.obj.Star2D;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class Gutil
{

	public static double G = 6.67428e-11;
	public static double s=1;//sec
	public static double m=60*s;
	public static double h=60*m;
	public static double d=h*24;
	public static double y=d*365.2425;
	public static double Ky = y*1000;
	public static double My = Ky*1000;
	
	public static Star2D translate2D(Star2D s, double x, double y, double a){
		double sen=Math.sin(a);
		double cos=Math.cos(a);
		double xx = (x+s.getX()*cos-s.getY()*sen);
		double yy=(y+s.getX()*sen+s.getY()*cos);
		double xxV = (x+s.getvX()*cos-s.getvY()*sen);
		double yyV=(y+s.getvX()*sen+s.getvY()*cos);
		s.setX(xx);
		s.setY(yy);
		s.setvX(xxV);
		s.setvY(yyV);		
		return s;
	}

	public static double distance (Body2D a, Body2D b){
		return Math.sqrt((a.getX()-b.getX())*(a.getX()-b.getX())+(a.getY()-b.getY())*(a.getY()-b.getY()));
	}
	
	public static double distance2 (Body2D a, Body2D b){
		return (a.getX()-b.getX())*(a.getX()-b.getX())+(a.getY()-b.getY())*(a.getY()-b.getY());
	}
	
	public static double angle(Body2D a, Body2D b){
//		System.out.println(Math.cos(Math.PI/4));
//		System.out.println(Math.sin(Math.PI/4));
//		System.out.println(Math.PI/4);
		double d = distance(a, b);
		double cosA= b.getX()/d;
		double sinA=b.getY()/d;
		double alpha =Math.atan2(sinA, cosA);
		return alpha;		
	}
	
	public static void writeArrayToFile(String path, ArrayList<?> array)
	{
		try
		{
			File file = new File(path);
			FileWriter fw = new FileWriter(file);
			BufferedWriter bw = new BufferedWriter(fw);
			for (int index = 0; index < array.size(); index++)
			{
				bw.append(array.get(index) + "\n");
			}
			bw.flush();
			bw.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		System.out.println(path +" Written");
	}
}
